<?php

use Drupal\shusy_express_type\Entity\ShusyExpressType;

function shusy_express_type_shusy_express_type_entity_form($form, &$form_state, ShusyExpressType $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_express_type') . '/shusy_express_type.forms.inc';
  $form_state[ShusyExpressType::class] = $e;


  $form['express_name'] = [
    '#type' => 'textfield',
    '#title' => t('快递类型'),
    '#maxlength' => 32,
    '#required' => TRUE,
    '#default_value' => $e->express_name,
    '#description' => t('快递类型必须唯一'),
  ];
  $form['express_code'] = [
    '#type' => 'textfield',
    '#title' => t('快递查询码'),
    '#maxlength' => 32,
    '#required' => TRUE,
    '#default_value' => $e->express_code,
    '#description' => t('详情查看 <a target="_blank" href="@link">@link</a>', ['@link' => 'https://market.aliyun.com/products/57126001/cmapi021863.html?spm=5176.2020520132.101.2.34e07218IEhzhY']),
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('启用'),
    '#default_value' => $e->status,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_express_type_shusy_express_type_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyExpressType::class];
  if ($e instanceof ShusyExpressType) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_express_type_shusy_express_type_delete_form($form, &$form_state, ShusyExpressType $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_express_type') . '/shusy_express_type.forms.inc';
  $form_state[ShusyExpressType::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_express_type/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_express_type_shusy_express_type_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyExpressType::class];
  if ($e instanceof ShusyExpressType) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_express_type';
}

